package com.qf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.support.BindingAwareModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

/**
 * 跳转视图
 * @author lixu
 */
@Controller
@RequestMapping("/view")//公共的路径的抽取
public class ViewController {


    /**
     * 跳转
     * 1、原始跳转方式
     */
    @RequestMapping("/test1")
    public void test1(HttpServletRequest request, HttpServletResponse response){

        try {
            request.setAttribute("name","小狗");
            //内部转发
            request.getRequestDispatcher("/hello.jsp").forward(request,response);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 2、跳转 : ModelAndView
     *      底层： request.getRequestDispatcher("/hello.jsp").forward(request,response);
     */
    @RequestMapping("/test2")
    public ModelAndView test2(){
        ModelAndView mav = new ModelAndView();
        //跳转hello.jsp
        //内部转发
        mav.setViewName("/hello.jsp");
        //数据
        mav.addObject("name","小狗");

        return mav;
    }
    /**
     * 3、跳转 : String
     *      底层： request.getRequestDispatcher("/hello.jsp").forward(request,response);
     */
    @RequestMapping("/test3")
    public String test3(Model model){
        model.addAttribute("name","小狗");
        //内部转发
        return "forward:/view/test4";
        //return "/hello.jsp";
        // jsp是不是Servlet
    }
    /**
     * 4、跳转 : 从当前这个方法跳转另外一个方法
     *
     */
    @RequestMapping("/test4")
    public String test4(Model model){
        model.addAttribute("name","大狗");
        return "redirect:/view/test5";
    }
    /**
     * 5、跳转 : 重定向了
     *
     */
    @RequestMapping("/test5")
    public String test5(Model model){
        model.addAttribute("name","超大大狗");
        return "/hello.jsp";
    }
    /**
     * 6、跳转 : .jsp页面路径有点长
     *
     */
    @RequestMapping("/test6")
    public String test6(Model model){
        model.addAttribute("name","list大狗");
        return "list";
    }
    /**
     * 100、跳转 : .jsp页面路径有点长
     *
     */
    @RequestMapping("/test100")
    public String test100(Model model){
        model.addAttribute("name","超大大狗");
        return "/WEB-INF/jsp/user/add.jsp";
    }

    /**
     *  携带数据到Jsp页面
     * @param BindingAwareModelMap: 带数据到Jsp页面 共有大约7个对象 全都是BindingAwareModelMap父类或是上层接口
     * @return
     */
    @RequestMapping("/test101")
    public String test101(BindingAwareModelMap map){
        //int i = 1/0;
        map.put("name","map狗");
        System.out.println("方法中==");
        return "user/add";
    }
}
